import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { PathParam } from "../config/pathParam";

export class Comment {
  id:number;
  type:string;
  fatherId:number;
  commentId:number;
  resCounts:number;
  userId:number;
  owner:string;
  time:number;
  timeStr:string;
  content:string;  
  response:Comment[];
}

@Injectable({
  providedIn: 'root'
})
export class CommentService {
  private commentURL:string = PathParam.base_url + 'api/comment/';

  constructor(private http: HttpClient) { }

  // searchMethod:可为fatherId/commentId/userId三种，分别返回按不同方式查找到的评论
  // type:评论所属的分类，如blog; 只有在使用fatherId方式查找时，type才有意义，其他方式可随意赋值。
  public GetComment(type:string, searchMethod:string, id:number){
    let jsonObj:Object = {};
    jsonObj["type"] = type;
    jsonObj[searchMethod] = id;
    return this.http.post(this.commentURL+'get', jsonObj);
  }

  public AddComment(type:string, fatherId:number, commentId:number, userId:number, content:string){
    let time = new Date().getTime().toString().substring(0, 10);  // 精确到秒
    let jsonObj = {"type":type, "fatherId":fatherId, "commentId":commentId, "userId":userId, "time":time, "content":content};
    return this.http.post(this.commentURL+'add', jsonObj, {responseType: "text"});
  }

  public UpdateComment(id:number, content:string){
    let jsonObj = {"id":id, "content":content};
    return this.http.post(this.commentURL+'update', jsonObj, {responseType: "text"});
  }

  public DeleteComment(id:number){
    let jsonObj = {"id":id};
    return this.http.post(this.commentURL+'delete', jsonObj, {responseType: "text"});
  }
}
